package com.gzyj.server.factory.jtt808.down;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gzyj.data.common.VehicleIncrement;
import com.gzyj.jtt808.utils.HexBytesUtil;
import com.gzyj.jtt808.v2011.protocol.terminalparam.*;
import com.gzyj.jtt808.v2013.Package;
import com.gzyj.server.config.LogConfig;
import com.gzyj.server.factory.CommonHandler;
import com.gzyj.server.factory.jtt808.NetWorkSerrvice;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class JTT808_0x8103_Handler implements CommonHandler {
    @Override
    public void handler(ChannelHandlerContext ctx, Package msg) throws Exception {

    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2019.Package msg) throws Exception {

    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2011.Package msg) {

    }

    @Autowired
    private NetWorkSerrvice netWorkSerrvice;

    @Override
    public void handler(VehicleIncrement vehicleInfo, String msg) {
        JSONObject command = JSON.parseObject(msg);

        String ipaddress = command.getString("ipAddress");
        String cmdId = command.getString("cmdID");
        Integer seq = Integer.parseUnsignedInt(command.getString("seq"));

        JSONArray array = JSON.parseArray(command.getString("paraList"));

        List<JTT808_0x8103_TerminalParam> paramList = new ArrayList<>();


        for (int i = 0; i < array.size(); i++) {
            JSONObject item = array.getJSONObject(i);

            Integer paraId = item.getInteger("cmdID");
            String paraValue = item.getString("paraValue");
            switch (paraId) {

                case 0x0002: {
                    JTT808_0x8103_0x0002 p = new JTT808_0x8103_0x0002();
                    p.setParamValue(Integer.valueOf(paraValue));
                    paramList.add(p);
                }
                break;
                case 0x0003: {
                    JTT808_0x8103_0x0003 p = new JTT808_0x8103_0x0003();
                    p.setParamValue(Integer.valueOf(paraValue));
                    paramList.add(p);
                }
                break;
                case 0x0004: {
                    JTT808_0x8103_0x0004 p = new JTT808_0x8103_0x0004();
                    p.setParamValue(Integer.valueOf(paraValue));
                    paramList.add(p);
                }
                break;
                case 0x0005: {
                    JTT808_0x8103_0x0005 p = new JTT808_0x8103_0x0005();
                    p.setParamValue(Integer.valueOf(paraValue));
                    paramList.add(p);
                }
                break;
                case 0x0006: {
                    JTT808_0x8103_0x0006 p = new JTT808_0x8103_0x0006();
                    p.setParamValue(Integer.valueOf(paraValue));
                    paramList.add(p);
                }
                break;
                case 0x0007: {
                    JTT808_0x8103_0x0007 p = new JTT808_0x8103_0x0007();
                    p.setParamValue(Integer.valueOf(paraValue));
                    paramList.add(p);
                }
                break;
                case 0x0010: {
                    JTT808_0x8103_0x0010 p = new JTT808_0x8103_0x0010();
                    p.setParamValue(paraValue);
                    paramList.add(p);
                }
                break;
                case 0x0011: {
                    JTT808_0x8103_0x0011 p = new JTT808_0x8103_0x0011();
                    p.setParamValue(paraValue);
                    paramList.add(p);
                }
                break;
                case 0x0012: {
                    JTT808_0x8103_0x0012 p = new JTT808_0x8103_0x0012();
                    p.setParamValue(paraValue);
                    paramList.add(p);
                }
                break;
                case 0x0013: {
                    JTT808_0x8103_0x0013 p = new JTT808_0x8103_0x0013();
                    p.setParamValue(paraValue);
                    paramList.add(p);
                }
                break;
                case 0x0014: {
                    JTT808_0x8103_0x0014 p = new JTT808_0x8103_0x0014();
                    p.setParamValue(paraValue);
                    paramList.add(p);
                }
                break;
                case 0x0015: {
                    JTT808_0x8103_0x0015 p = new JTT808_0x8103_0x0015();
                    p.setParamValue(paraValue);
                    paramList.add(p);
                }
                break;
                case 0x0016: {
                    JTT808_0x8103_0x0016 p = new JTT808_0x8103_0x0016();
                    p.setParamValue(paraValue);
                    paramList.add(p);
                }
                break;
                case 0x0017: {
                    JTT808_0x8103_0x0017 p = new JTT808_0x8103_0x0017();
                    p.setParamValue(paraValue);
                    paramList.add(p);
                }
                break;


            }

        }

        if (StringUtils.isNotEmpty(vehicleInfo.getTerminalprotocoltype()) && vehicleInfo.getTerminalprotocoltype().equalsIgnoreCase("2019")) {


        } else {
            com.gzyj.jtt808.v2013.protocol.JTT808_0x8103 jtt808_0x8103 = new com.gzyj.jtt808.v2013.protocol.JTT808_0x8103();
            com.gzyj.jtt808.v2013.HeaderMsgBodyProperty headerMsgBodyProperty = new com.gzyj.jtt808.v2013.HeaderMsgBodyProperty();
            headerMsgBodyProperty.setSubcontract(false);
            headerMsgBodyProperty.setEncryptMethod(com.gzyj.jtt808.v2013.HeaderEncryptMethod.None);
            jtt808_0x8103.getPackageHeader().setMsgBodyProperty(headerMsgBodyProperty);
            jtt808_0x8103.getPackageHeader().setTerminalPhoneNo(ipaddress);

            jtt808_0x8103.setTerminalParams(paramList);

            ByteBuf data = jtt808_0x8103.encode();

            netWorkSerrvice.send2Veh(ipaddress, data);
            LogConfig.commondLog.info("808 2013版 8013 下发 设置终端参数 指令 {} {}", ipaddress, HexBytesUtil.bytesToHex(data.array()));


        }


    }
}
